<template>
  <div class="login-page">
    <el-card class="login-box">
      <header>黑马面经运营后台</header>
      <el-form
        class="pp"
        :label-position="labelPosition"
        :rules="rules"
        label-width="80px"
        :model="formData"
        ref="loginForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="formData.password"></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="submit">登录</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login } from '@/api/user';

export default {
  name: 'login-page',
  props: {},
  components: {},
  data() {
    return {
      labelPosition: 'top',
      formData: {
        username: 'admin8',
        password: 'admin',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            pattern: /^[0-9a-z]{3,10}/i, message: '长度在 3 到 5 个字符', trigger: 'blur',
          }],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3, max: 11, message: '长度在 3 到 5 个字符', trigger: 'blur',
          }],
      },
    };
  },
  computed: {},
  watch: {},
  created() {
    // login({
    //   username: 'admin8',
    //   password: 'admin',
    // });
  },
  mounted() {},
  methods: {
    submit() {
      this.$refs.loginForm.validate(async (flag) => {
        if (!flag) return;
        // 发送请求
        const { data } = await login(this.formData);
        console.log(login(this.formData));
        // console.log(data.token);
        console.log(data);
        this.$store.commit('SET_TOKEN', data.token);
        this.$router.push('/layout');
      });
      // console.log(22);
    },
  },
};
</script>

<style lang="less" scoped>
.login-page {
  // css中使用~@ 代表 src根目录
  background: url(~@/assets/svg/login-bg.svg) no-repeat;
  background-position: center; //如果只有一个center就代表水平，垂直都居中
  // cover覆盖 contain包含
  background-size: cover;
  height: 100vh;
}
// 登录 样式穿透
::v-deep {
  .el-card__body,
  .el-main {
    padding: 0 !important;
  }
}

.login-box {
  height: 390px;
  width: 422px;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  header {
    height: 80px;
    background-color: #727cf5;
    color: white;
    line-height: 80px;
    font-size: 18px;
    text-align: center;
  }
  // form 表单样式
  .pp {
    padding: 0px 40px;
    margin-top: 20px;
  }
  .btn {
    margin-top: 20px;
    text-align: center;
  }
  ::v-deep {
    .el-form--label-top .el-form-item__label {
      padding: 0 !important;
    }
    .el-form-item {
      margin-bottom: 10px;
    }
  }
}
</style>
